#include "ScenesManager.h"
#include "source/input/InputSourceManager.h"
#include "source/output/OutputSourceManager.h"
#include "Util/logc.h"
#include "Util/util.h"

//#include "frame_common.h"

namespace Media{

bool testRtspAddInputSource(const std::string& content){
    inputSourceTable tb;
    uint32_t id;
    tb.m_con_type = "network";
    tb.m_content = content;//"rtsp://192.168.17.214:554/1"; //"rtsp://192.168.16.30/live/mainstream";
    tb.m_custom = true;
    tb.m_type = 0;
    tb.m_name = "test_input_rtsp1";

    if(InputSourceManager::Instance()->AddSource(tb, id)){
        tracef("add rtsp source success! id=%u",id);
        return true;
    }else{
        errorf("add rtsp source failed!");
        return false;
    }
}


bool testHdmiAddInputSource(){
    inputSourceTable tb;
    uint32_t id;
    tb.m_con_type = HDMI_TYPE;
    tb.m_content = "hdmi in";
    tb.m_custom = false;
    tb.m_type = 0;
    tb.m_name = "hdmi in";

    if(InputSourceManager::Instance()->AddSource(tb, id)){
        tracef("add hdmi source success! id=%u",id);
        return true;
    }else{
        errorf("add hdmi source failed!");
        return false;
    }
}

bool testAddOutputSource(uint32_t& id){
    outputSourceTable tb = {};
    tb.m_con_type = HDMI_TYPE;//"network";
    tb.m_content = "rtsp://192.168.16.59:554/live/push1";
    tb.m_custom = false;
    tb.m_type = 0;
    tb.m_name = "hdmi_out";  //"test_rtsp1";

    if(OutputSourceManager::Instance()->AddSource(tb, id)){
        tracef("add  output success! id=%u",id);
        return true;
    }else{
        errorf("add  output failed!");
        return false;
    }
}

bool testScenes(){
    uint32_t scenes_id = 1;
    std::string err_info;

    if(!ScenesManager::Instance()->AddScenes("test_scenes", scenes_id, err_info)){
        errorf("AddScenes fail %s", err_info.c_str());
    }

    // if(ScenesManager::Instance()->AddScenes("test_scenes", scenes_id, err_info)){
    tracef("add scenes success! id=%u",scenes_id);

#if 1
    //add videoItem
    VideoScenesInItemConfig config;
    config.m_x = 0;
    config.m_y = 0;
    config.m_width = 640;//1920;
    config.m_height = 360;//1080;
    config.m_sourceId = 1;
    config.m_layers = 1;
    config.m_content = "rtsp://192.168.17.214:554/1";
    uint32_t itemId;
    if(ScenesManager::Instance()->AddVideoInItem(scenes_id, config, itemId)){
        tracef("add videoitem success! id=%u",itemId);
    }else{
        errorf("add videoitem failed! %s",err_info.c_str());
    }
#endif
#if 1
{
    //add videoItem
    VideoScenesInItemConfig config1;
    config1.m_x = 640;
    config1.m_y = 360;
    config1.m_width = 640;
    config1.m_height = 360;
    config1.m_sourceId = 2;
    config1.m_layers = 1;
    config1.m_content = "rtsp://192.168.17.214:554/1";
    uint32_t itemId1;
    if(ScenesManager::Instance()->AddVideoInItem(scenes_id, config1, itemId1)){
     tracef("add videoitem success! id=%u",itemId1);
    }else{
     errorf("add videoitem failed! %s",err_info.c_str());
    }
}
#endif



    VideoScenesOutItemConfig oconfig = {};
    oconfig.m_codec = "H264";
    oconfig.m_frameRate = 30;
    oconfig.m_width = 1920;
    oconfig.m_height = 1080;
    ScenesManager::Instance()->SetVideoOutConfig(scenes_id, oconfig);


    //add outPutSource
    uint32_t  out_id;
    testAddOutputSource(out_id);
    ScenesManager::Instance()->AddVideoOutSource(scenes_id, out_id);

    //start
    ScenesManager::Instance()->StartScenes(scenes_id);


    std::this_thread::sleep_for(std::chrono::seconds(30));

    return true;
    // tracef("StopScenes ");
    //stop
    ScenesManager::Instance()->StopScenes(scenes_id);


    // tracef("DelScenes ");
    //remove scenes
    ScenesManager::Instance()->DelScenes(scenes_id);

    return true;
}



void TestScenes(){
    tracef("######### TestScenes");
//    std::this_thread::sleep_for(std::chrono::seconds(10));
    ScenesManager::Instance();
    InputSourceManager::Instance();
    OutputSourceManager::Instance();
    // auto player = FrameCommon::Create("http://192.168.11.45/11.mp4");

//    testAddInputSource("rtsp://192.168.17.214:554/1");

//    testAddInputSource("rtsp://192.168.17.214:554/1");
    testRtspAddInputSource("rtsp://192.168.17.92:554/1");
//    testRtspAddInputSource("rtsp://192.168.16.30/live/mainstream");
    testHdmiAddInputSource();
//    rtsp://192.168.19.143/live/mainstream
//    testAddInputSource("rtsp://192.168.19.143/live/mainstream");
    testScenes();
}

bool testStartScenes(){

    return true;
} 

bool testStopScenes(){

    return true;
}


}